home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / RKMLibsPrgs / graphics_libraries / sprites_bobs / ssprite.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  6KB  |  148 lines

  1. /* ssprite.c - Simple Sprite example
  2. **
  3. ** SAS/C V5.10a
  4. ** lc -b1 -cfist -v -y ssprite.c
  5. ** blink FROM LIB:c.o ssprite.o LIB LIB:lc.lib LIB:amiga.lib TO ssprite
  6.  
  7.  
  8. Copyright (c) 1992 Commodore-Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Commodore-Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Commodore Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33. #include <exec/types.h>
  34. #include <graphics/gfx.h>
  35. #include <graphics/gfxbase.h>
  36. #include <graphics/gfxmacros.h>
  37. #include <graphics/sprite.h>
  38. #include <intuition/intuitionbase.h>
  39. #include <intuition/screens.h>
  40. #include <hardware/custom.h>
  41. #include <hardware/dmabits.h>
  42. #include <libraries/dos.h>
  43.  
  44. #include <clib/graphics_protos.h>
  45. #include <clib/exec_protos.h>
  46. #include <clib/intuition_protos.h>
  47. #include <clib/alib_stdio_protos.h>
  48.  
  49. #include <stdlib.h>
  50.  
  51. struct GfxBase *GfxBase = NULL;
  52. struct IntuitionBase *IntuitionBase = NULL;
  53. extern struct Custom far custom ;
  54.  
  55. /* real boring sprite data */
  56. UWORD chip sprite_data[ ] = {
  57.     0, 0,           /* position control           */
  58.     0xffff, 0x0000, /* image data line 1, color 1 */
  59.     0xffff, 0x0000, /* image data line 2, color 1 */
  60.     0x0000, 0xffff, /* image data line 3, color 2 */
  61.     0x0000, 0xffff, /* image data line 4, color 2 */
  62.     0x0000, 0x0000, /* image data line 5, transparent */
  63.     0x0000, 0xffff, /* image data line 6, color 2 */
  64.     0x0000, 0xffff, /* image data line 7, color 2 */
  65.     0xffff, 0xffff, /* image data line 8, color 3 */
  66.     0xffff, 0xffff, /* image data line 9, color 3 */
  67.     0, 0            /* reserved, must init to 0 0 */
  68.     };
  69.  
  70. VOID main(int argc, char **argv)
  71. {
  72. struct SimpleSprite    sprite = {0};
  73. struct ViewPort        *viewport;
  74.  
  75. WORD sprite_num;
  76. SHORT delta_move, ktr1, ktr2, color_reg;
  77. struct Screen *screen;
  78. int return_code;
  79.  
  80. return_code = RETURN_OK;
  81.  
  82. if (NULL == (GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",37L)))
  83.     return_code = RETURN_FAIL;
  84. else
  85.     {
  86.     if (NULL == (IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",37L)))
  87.         return_code = RETURN_FAIL;
  88.     else
  89.         {
  90.         /* opened library, need a viewport to render a sprite over. */
  91.         if (NULL == (screen = OpenScreenTagList(NULL, NULL)))
  92.             return_code = RETURN_FAIL;
  93.         else
  94.             {
  95.             viewport = &screen->ViewPort;
  96.             if (-1 == (sprite_num = GetSprite(&sprite, 2)))
  97.                 return_code = RETURN_WARN;
  98.             else
  99.                 {
  100.                 /* Calculate the correct base color register number, */
  101.                 /* set up the color registers.                       */
  102.                 color_reg = 16 + ((sprite_num & 0x06) << 1);
  103.                 printf("color_reg=%d\n", color_reg);
  104.                 SetRGB4(viewport, color_reg + 1, 12,  3,  8);
  105.                 SetRGB4(viewport, color_reg + 2, 13, 13, 13);
  106.                 SetRGB4(viewport, color_reg + 3,  4,  4, 15);
  107.  
  108.                 sprite.x = 0;       /* initialize position and size info    */
  109.                 sprite.y = 0;       /* to match that shown in sprite_data   */
  110.                 sprite.height = 9;  /* so system knows layout of data later */
  111.  
  112.                 /* install sprite data and move sprite to start position. */
  113.                 ChangeSprite(NULL, &sprite, (APTR)sprite_data);
  114.                 MoveSprite(NULL, &sprite, 30, 0);
  115.  
  116.                 /* move the sprite back and forth. */
  117.                 for ( ktr1 = 0, delta_move = 1;
  118.                       ktr1 < 6; ktr1++, delta_move = -delta_move)
  119.                     {
  120.                     for ( ktr2 = 0; ktr2 < 100; ktr2++)
  121.                         {
  122.                         MoveSprite( NULL, &sprite, (LONG)(sprite.x + delta_move),
  123.                                     (LONG)(sprite.y + delta_move) );
  124.                         WaitTOF(); /* one move per video frame */
  125.  
  126.                         /* Show the effect of turning off sprite DMA. */
  127.                         if (ktr2 == 40) OFF_SPRITE ;
  128.                         if (ktr2 == 60) ON_SPRITE ;
  129.                         }
  130.                     }
  131.                 /* NOTE:  if you turn off the sprite at the wrong time (when it
  132.                 ** is being displayed), the sprite will appear as a vertical bar
  133.                 ** on the screen.  To really get rid of the sprite, you must
  134.                 ** OFF_SPRITE while it is not displayed.  This is hard in a
  135.                 ** multi-tasking system (the solution is not addressed in
  136.                 ** this program).
  137.                 */
  138.                 ON_SPRITE ; /* just to be sure */
  139.                 FreeSprite((WORD)sprite_num);
  140.                 }
  141.             (VOID) CloseScreen(screen);
  142.             }
  143.         CloseLibrary((struct Library *)IntuitionBase);
  144.         }
  145.     CloseLibrary((struct Library *)GfxBase);
  146.     }
  147. exit(return_code);
  148. }